package cn.lkx.eduservice.service.impl;

import cn.lkx.eduservice.entity.EduCourse;
import cn.lkx.eduservice.entity.EduCourseDescription;
import cn.lkx.eduservice.entity.vo.CourseInfoVo;
import cn.lkx.eduservice.entity.vo.CoursePublishVo;
import cn.lkx.eduservice.entity.vo.CourseQuery;
import cn.lkx.eduservice.entity.vo.frontvo.CourseDetailVo;
import cn.lkx.eduservice.entity.vo.frontvo.CourseQueryVo;
import cn.lkx.eduservice.mapper.EduCourseMapper;
import cn.lkx.eduservice.service.EduChapterService;
import cn.lkx.eduservice.service.EduCourseDescriptionService;
import cn.lkx.eduservice.service.EduCourseService;
import cn.lkx.eduservice.service.EduVideoService;
import cn.lkx.servicebase.exceptionhandle.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lkx
 * @since 2022-05-03
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //要操作courseDescription 这张表 添加简介
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //删除课程需要先删除小节和章节
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private EduVideoService videoService;

    //保存课程基本信息 （操作两张表）
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1、向课程表添加基本信息
         //将CourseInfoVo 转换成 EduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0){
            //如果添加失败
            throw new MyException(20001,"添加课程信息失败！");
        }
        //课程添加成功后，获取他的id
        String cid = eduCourse.getId();
        //2、向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid); //和课程一一对应，记得改实体类主键生成策略
        boolean save = courseDescriptionService.save(eduCourseDescription);//保存

        //返回课程id
        return cid;
    }

    //后台根据id查询基本信息 返回的是CourseInfoVo 包含简介
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1、查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();  //封装成courseInfoVo
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //2、查询乘客简介表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }
    //修改课程信息 同样操作两张表
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1、修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);  //根据id更新
        if (update == 0){
            throw new MyException(20001,"课程信息修改失败！");
        }
        //2、修改简介表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId()); //设置id
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(eduCourseDescription); //根据id更新
    }

    //根据id查询到 最终发布课程的确认信息
    @Override
    public CoursePublishVo getCoursePublishInfo(String id) {
        return baseMapper.getPublishCourseInfo(id);
    }

    //后台分页且多条件查询课程信息
    @Override
    public void pageQueryConditions(Page<EduCourse> page, CourseQuery courseQuery) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        String status = courseQuery.getStatus();
        if( !StringUtils.isEmpty(title)){  //条件不为空，加入wrapper
            queryWrapper.like("title",title);
        }
        if ( !StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("teacher_id",teacherId); //数据库的字段名,不是属性名
        }
        if ( !StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id",subjectParentId);
        }
        if ( !StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id",subjectId);
        }
        if ( !StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        queryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(page,queryWrapper);
    }

    //删除课程
    @Override
    public boolean removeCourseById(String courseId) {
        //1、删除小节
        videoService.removeByCourseId(courseId);
        //2、删除章节
        chapterService.removeByCourseId(courseId);
        //3、删除课程
        int delete = baseMapper.deleteById(courseId);
        if(delete==0){
            throw new MyException(20001,"删除课程失败！");
        }
        return delete>0;
    }

    //前台多条件查询课程列表
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseQueryVo courseQueryVo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        //拼接查询条件
        if ( !StringUtils.isEmpty(courseQueryVo.getSubjectParentId())){//一级分类
            queryWrapper.eq("subject_parent_id",courseQueryVo.getSubjectParentId());
        }
        if ( !StringUtils.isEmpty(courseQueryVo.getSubjectId())){//二级分类
            queryWrapper.eq("subject_id",courseQueryVo.getSubjectId());
        }
        if ( !StringUtils.isEmpty(courseQueryVo.getBuyCountSort())){//购买数 （关注度）
            queryWrapper.orderByDesc("buy_count");
        }
        if ( !StringUtils.isEmpty(courseQueryVo.getGmtCreateSort())){//创建时间
            queryWrapper.orderByDesc("gmt_create");
        }
        if ( !StringUtils.isEmpty(courseQueryVo.getGmtCreateSort())){//价格
            queryWrapper.orderByDesc("price");
        }
        //查
        baseMapper.selectPage(coursePage,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        //封装结果
        map.put("items",coursePage.getRecords());
        map.put("pages",coursePage.getPages());
        map.put("current",coursePage.getCurrent());
        map.put("total",coursePage.getTotal());
        map.put("hasNext",coursePage.hasNext());
        map.put("hasPrevious",coursePage.hasPrevious());
        map.put("size", coursePage.getSize());
        return map;
    }
    //根据课程id查询课程详情页信息
    @Override
    public CourseDetailVo getCourseDetailById(String courseId) {
        return baseMapper.getCourseDetailById(courseId);
    }
}
